import sys
import sched
import time
import threading
import random


class timerSched:
    def __init__(self):
        self.sched = sched.scheduler(time.time, time.sleep)
        pass

    def add_timer(self, delay, action, argument=(), kwargs={}):
        self.sched.enter(delay, 1, action, argument, kwargs)
        pass

    def loop_run(self):
        while True:
            print("loop run")
            self.sched.run()
            time.sleep(2)


def print_time(id="default"):
    print("print time:", time.time(), id)


def cmd_control(time_sched):
    help_str = """
1 添加定时任务
2 删除定时任务
q 退出
"""
    name_list = ["lilisi", "Lucy", "Malike", "Jack", "Pite", "Wali", "Yiwa"]
    event_list = []
    while True:
        cmd = input(help_str)
        if cmd == "1":
            delay = int(input("请输入延迟时间："))
            id = name_list[random.randint(0, len(name_list) - 1)]
            evnet = time_sched.add_timer(delay, print_time, (id,))
            event_list.append(evnet)
            print(f"id={id} 添加成功, start ----")
        elif cmd == "2":
            print("event 为 None , 无法判断")
        elif cmd == "q":
            break

    sys.exit(0)


if __name__ == "__main__":
    time_sched = timerSched()
    t1 = threading.Thread(target=time_sched.loop_run)
    t1.start()
    cmd_control(time_sched)

""" 
id=Pite 添加成功, start ----
id=Yiwa 添加成功, start ----

print time: 1733986167.1066377 Pite
print time: 1733986169.583664 Yiwa
"""